home *** CD-ROM | disk | FTP | other *** search
- Path: news.algonet.se!usenet
- From: bjorn@algonet.se (Bjorn Fahller)
- Newsgroups: comp.lang.c++
- Subject: Re: Callbacks using member functions
- Date: Sun, 28 Jan 1996 01:09:48 +0100
- Organization: K-os
- Message-ID: <M7rCxsNapkOO089yn@algonet.se>
- References: <4dgkke$8mf@knot.queensu.ca>
- NNTP-Posting-Host: sophocles.algonet.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
-
- In article <4dgkke$8mf@knot.queensu.ca>,
- 3mal5@qlink.queensu.ca (Lepage Marc A) wrote:
-
- [asking about methods as callbacks]
-
- Quite a few have pointed out why it is not possible to have a
- method (that is not static) as a callback.
-
- As every so often, I'm going in the other direction, and say that
- I often do what you want to do. It's not always that easy thought,
- and in your case, you may need to write a wrapper class for
- Motif (by the way, Rogue Wave's View.h++ is nice, although
- it defeats type checking with its method based callbacks.)
-
- A while ago, almost a year ago, there was an article in "C++ Report"
- about a template based callback mechanism, that cared little if the
- callback was a function, or the combination of an object and a method.
-
- The solution looked great on the surface, but hid some rather naughty
- things when examining it closer (It was very cleverly thougth out,
- though, and I use it a lot at home, since using it is so clean!)
-
- All in all, what you need to do, is to tie both a method and
- a pointer to an object, as the callback. The only way I can see
- to do that, with your Motif callback, is to write a class (or function
- combo) like this:
-
- template <class Callback>
- class callback
- {
- public:
- static void set(Callback*, RetType (Callback*)::method(ArgTypes));
- static RetType cb(ArgTypes);
- private:
- static RetType (Callback*)::m(ArgTypes);
- static Callback* p;
- }
-
- template <class Callback>
- void callback<Callback>::set(Callback* po, RetType (Callback*)::method(ArgTypes))
- {
- p = po;
- m = method;
- // do whatever to register callback::cb as the callback function.
- }
-
- template <class Callback>
- RetType callback<Callback>::cb(ArgTypes args)
- {
- return (p->m)(args);
- }
-
- Where RetType is the required type of the callback function,
- and ArgTypes is the type of the argument (or several, if so is
- needed.)
-
- Hope this helps.
- _
- /Bjorn. (PS, the example above is not tested, so errors are more
- likely than not.)
- --
- Bjorn Fahller | Phone W: +46 8 4220898 | General Purpose Guru
- Siljansvagen 35 | H: +46 8 918297 | and Problem Solver
- S-120 55 Arsta/SWEDEN | Finger me for PGP Key | Extraordinaire
- PGP Key fingerprint = 7E 3D 1B E2 3F D4 E8 0C E2 82 AB 81 E4 18 BF 39
-
-